%this is spatial average filtering
clc;
clear all;
close all;

A = imread('cameraman.tif');
A = imnoise(A,'salt & pepper',0.02);
imshow(A);
title('Original Image');
% [m,n]= size(A);
A = double(A);

mask = (1/9)*([1 1 1; 1 1 1; 1 1 1]);


A = padarray(A,[1,1]);

[m,n]= size(A);
B = zeros(m,n);

for i = 2:m-1
    for j = 2:n-1
        C = ([A(i-1,j-1) A(i-1,j) A(i-1,j+1);(A(i,j-1)) (A(i,j)) (A(i,j+1)); A(i+1,j-1) A(i+1,j) A(i+1,j+1)]);
        D = C*mask;
        B(i,j) = mean(D(:));
%         B(i,j) = sum(D(:))/9;
    end
end

title('Modified Image');
figure,imshow(uint8(B));